﻿//////////////////////////////////////////////////////////////////
//
// Hester\Account.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sub-license, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using DanPike.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hester
{
   public class Account
   {
      private static Logger Logger = Program.Logger;
      public readonly string Name;
      private Dictionary<Folder, Dictionary<string, Server>> folders_ = new Dictionary<Folder, Dictionary<string, Server>>();
      private Dictionary<Folder, ConfigCache> configCaches_ = new Dictionary<Folder, ConfigCache>();

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      public class FolderAddedEventArgs : EventArgs
      {
         public Folder Folder;
      }
      public delegate void FolderAddedEventHandler(object sender, FolderAddedEventArgs eventArgs);
      public FolderAddedEventHandler FolderAdded;

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="accountName"></param>
      internal Account(string accountName)
      {
         Name = accountName;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="folder"></param>
      public void AddFolder(Folder folder)
      {
         try
         {
            Dictionary<string, Server> serverMap = new Dictionary<string, Server>(StringComparer.CurrentCultureIgnoreCase);
            folders_.Add(folder, serverMap);

            if (null != FolderAdded)
            {
               FolderAddedEventArgs eventArgs = new FolderAddedEventArgs();
               eventArgs.Folder = folder;
               FolderAdded(this, eventArgs);
            }

            ConfigCache configCache = new ConfigCache();
            configCache.loadFile(this, folder);
            configCaches_.Add(folder, configCache);
         }
         catch (System.Exception ex)
         {
            Logger.Exception(ex);
         }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="folder"></param>
      /// <param name="server"></param>
      public void linkServer(Folder folder, Server server)
      {
         try
         {
            folders_[folder].Add(server.Name, server);
         }
         catch (System.Exception ex)
         {
            Logger.Exception(ex);
         }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="folder"></param>
      /// <param name="server"></param>
      public void Show(ListView view, Folder folder, Toons toons)
      {
         try
         {
            Dictionary<string, Server> servers = folders_[folder];
            servers.Values.ToList().ForEach(server =>
               toons.getServerToons(Name, server.Name).ForEach(toon =>
                  toon.Show(view, this, server)));
         }
         catch (System.Exception ex)
         {
            Logger.Exception(ex);
         }
      }
   }
}
